package com.property.cleaning.service.impl;

import com.property.cleaning.common.Result;
import com.property.cleaning.common.ResultCode;
import com.property.cleaning.entity.User;
import com.property.cleaning.mapper.UserMapper;
import com.property.cleaning.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务实现类
 * 
 * @author Property Cleaning System
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<String> login(String username, String password) {
        if (username == null || username.trim().isEmpty() || password == null || password.trim().isEmpty()) {
            return Result.error(ResultCode.PARAM_ERROR, "用户名或密码不能为空");
        }

        User user = userMapper.findByUsername(username);
        if (user == null) {
            return Result.error(ResultCode.USER_NOT_FOUND, "用户不存在");
        }

        if (!password.equals(user.getPassword())) {
            return Result.error(ResultCode.PASSWORD_ERROR, "密码错误");
        }

        if (user.getStatus() == 0) {
            return Result.error(ResultCode.USER_DISABLED, "用户已被禁用");
        }

        return Result.success("登录成功");
    }

    @Override
    public Result<User> register(User user) {
        if (user == null || user.getUsername() == null || user.getUsername().trim().isEmpty()) {
            return Result.error(ResultCode.PARAM_ERROR, "用户名不能为空");
        }

        if (user.getPassword() == null || user.getPassword().trim().isEmpty()) {
            return Result.error(ResultCode.PARAM_ERROR, "密码不能为空");
        }

        // 检查用户名是否已存在
        User existingUser = userMapper.findByUsername(user.getUsername());
        if (existingUser != null) {
            return Result.error(ResultCode.USERNAME_EXISTS, "用户名已存在");
        }

        // 检查邮箱是否已存在
        if (user.getEmail() != null && !user.getEmail().trim().isEmpty()) {
            existingUser = userMapper.findByEmail(user.getEmail());
            if (existingUser != null) {
                return Result.error(ResultCode.EMAIL_EXISTS, "邮箱已存在");
            }
        }

        // 检查手机号是否已存在
        if (user.getPhone() != null && !user.getPhone().trim().isEmpty()) {
            existingUser = userMapper.findByPhone(user.getPhone());
            if (existingUser != null) {
                return Result.error(ResultCode.PHONE_EXISTS, "手机号已存在");
            }
        }

        userMapper.insert(user);
        return Result.success(user);
    }

    @Override
    public Result<User> getUserInfo(Long userId) {
        if (userId == null) {
            return Result.error(ResultCode.PARAM_ERROR, "用户ID不能为空");
        }

        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error(ResultCode.USER_NOT_FOUND, "用户不存在");
        }

        return Result.success(user);
    }

    @Override
    public Result<com.baomidou.mybatisplus.core.metadata.IPage<User>> getUserPage(com.baomidou.mybatisplus.extension.plugins.pagination.Page<User> page, String keyword, Integer status) {
        return Result.success(null);
    }

    @Override
    public Result<User> updateUser(User user) {
        if (user == null || user.getId() == null) {
            return Result.error(ResultCode.PARAM_ERROR, "用户ID不能为空");
        }

        User existingUser = userMapper.selectById(user.getId());
        if (existingUser == null) {
            return Result.error(ResultCode.USER_NOT_FOUND, "用户不存在");
        }

        userMapper.updateById(user);
        return Result.success(user);
    }

    @Override
    public Result<Void> deleteUser(Long userId) {
        if (userId == null) {
            return Result.error(ResultCode.PARAM_ERROR, "用户ID不能为空");
        }

        userMapper.deleteById(userId);
        return Result.success();
    }

    @Override
    public Result<Void> changePassword(Long userId, String oldPassword, String newPassword) {
        if (userId == null || oldPassword == null || newPassword == null) {
            return Result.error(ResultCode.PARAM_ERROR, "参数不能为空");
        }

        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error(ResultCode.USER_NOT_FOUND, "用户不存在");
        }

        if (!oldPassword.equals(user.getPassword())) {
            return Result.error(ResultCode.PASSWORD_ERROR, "原密码错误");
        }

        user.setPassword(newPassword);
        userMapper.updateById(user);
        return Result.success();
    }

    @Override
    public Result<Void> resetPassword(Long userId, String newPassword) {
        if (userId == null || newPassword == null) {
            return Result.error(ResultCode.PARAM_ERROR, "参数不能为空");
        }

        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error(ResultCode.USER_NOT_FOUND, "用户不存在");
        }

        user.setPassword(newPassword);
        userMapper.updateById(user);
        return Result.success();
    }
}